package com.app.controller;

import cn.hutool.http.HttpUtil;
import com.app.processor.SsoServerProcesssor;
import com.portal.sso.core.server.VerificationTgtServer;
import com.portal.sso.core.util.PortalSecureUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
public class LoginController {
    @Autowired
    SsoServerProcesssor ssoServerProcesssor;
    @Autowired
    VerificationTgtServer verificationTgtServer;
    @GetMapping("/login")
    public String Login(){
        String userId = "admin";
        String pwd = "123";
        String encryKey = "no";
        //开启加密时，加密传输，使用AES加密方式
        if(ssoServerProcesssor.isSsoEncryKey()){
            userId = PortalSecureUtil.AesEncode(userId,ssoServerProcesssor.getClientSecret());
            pwd = PortalSecureUtil.AesEncode(pwd,ssoServerProcesssor.getClientSecret());
            encryKey = "yes";
        }
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("username", userId);
        paramMap.put("password", pwd);
        paramMap.put("encryKey",encryKey);

        String url = ssoServerProcesssor.getServer()+"/app/login";
        String result= HttpUtil.post(url, paramMap);

        return  result;
    }
    @GetMapping("/check")
    public String Check(String tgt){
        String url = ssoServerProcesssor.getServer()+"/verification";
        //TODO: 实际使用中，下面的逻辑可以自己实现，不需要引用core组件。这里为了测试偷了些懒，这样就做到了完全脱离sso组件，就可以接入到认证中心。
        String userId =  verificationTgtServer.Verification(url,tgt);
        return  userId;
    }

}
